

$(function (){

    $('#change').on('click',function (e){
       e.preventDefault();
       $.ajax({
           type     : 'POST',
            url     : 'http://localhost:8080/changeMyAccount',
           data    : {
               username            : $('#username').val(),
               password            : $('#password').val(),
               repeatPassword     : $('#repeatPassword').val(),
               firstname           : $('#firstname').val(),
               lastname            : $('#lastname').val(),
               email               : $('#email').val(),
               token               : $('#token').val(),
               country             : $('#country').val(),
               state               : $('#state').val(),
               city                : $('#city').val(),
               address1            : $('#address1').val(),
               address2            : $('#address2').val(),
               zip                 : $('#zip').val(),
               phone               : $('#phone').val(),
               favcategory         : $('#favcategory').val()
           },
           success  : function (data){
               if(data.indexOf("address") !== -1){
                   console.log(data);
                   window.location.href='http://localhost:8080/' + data.substring(data.indexOf(":") + 1, data.length);
               }else{
                   //显示
                   $('#error-msg').text(data);
                   console.log('register ='+data);
               }
           },
           error    : function (errorMsg){
               console.log(errorMsg);
           }
       });
    });



    $('#username').on('blur',function(){
        var username    = $(this).val();
        var isValidate  = checkUsername(username);
        if(isValidate !== ''){
            $('#error-msg').text(isValidate);
        }else{
            $('#error-msg').text('');
        }
    });
    $('#password').on('blur',function(){
        var password    = $(this).val();
        if(password === ''){
            $('#error-msg').text('The password cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
    $('#repeatPassword').on('blur',function(){
        var repeatePassword    = $(this).val();
        var password    = $('#password').val();
        if(repeatePassword === ''){
            $('#error-msg').text('The repeat password cannot be empty');
        }else if(repeatePassword !== password){
            $('#error-msg').text('The password is not the same between the two entries');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#firstname').on('blur',function(){
        var firstname    = $(this).val();
        if(firstname === ''){
            $('#error-msg').text('The firstname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
    $('#lastname').on('blur',function(){
        var lastname    = $(this).val();
        if(lastname === ''){
            $('#error-msg').text('The lastname cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#email').on('blur',function(){
        var email    = $(this).val();
        if(email === ''){
            $('#error-msg').text('The email cannot be empty');
        }else if(email.indexOf('@') === -1){
            $('#error-msg').text('Please enter a reasonable email');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#token').on('blur',function(){
        var token    = $(this).val();
        //检测正确性要提交之后。
        if(code === ''){
            $('#error-msg').text('Please get a verification code first');
        }else if(token === ''){
            $('#error-msg').text('The token cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#country').on('blur',function(){
        var country    = $(this).val();
        if(country === ''){
            $('#error-msg').text('The country cannot be empty');
        }
    });

    $('#state').on('blur',function(){
        var state    = $(this).val();
        if(state === ''){
            $('#error-msg').text('The state cannot be empty');
        }
    });
    $('#city').on('blur',function(){
        var city    = $(this).val();
        if(city === ''){
            $('#error-msg').text('The city cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#address1').on('blur',function(){
        var address1    = $(this).val();
        if(address1 === ''){
            $('#error-msg').text('The address1 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#address2').on('blur',function(){
        var address2    = $(this).val();
        if(address2 === ''){
            $('#error-msg').text('The address2 cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#zip').on('blur',function(){
        var zip    = $(this).val();
        if(zip === ''){
            $('#error-msg').text('The zip cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#phone').on('blur',function(){
        var phone    = $(this).val();
        if(phone === ''){
            $('#error-msg').text('The phone cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });

    $('#favcategory').on('blur',function(){
        var favcategory    = $(this).val();
        if(favcategory === 'not'){
            $('#error-msg').text('The favourite category cannot be empty');
        }else{
            $('#error-msg').text('');
        }
    });
})